﻿using System.Data;
using System.IO;

namespace Gdmec.Util
{
    public class ExcelUtil
    {
        /// <summary>
        /// 读取Excel
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static DataSet LoadExcelToDataSet(string path)
        {
            using (var streamFile = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                DataSet ds = null;
                var extension = Path.GetExtension(path).ToLower();
                //IExcelDataReader reader = null;
                //if (extension == ".xls")
                //{
                //    reader = ExcelReaderFactory.CreateBinaryReader(streamFile);
                //}
                //else if (extension == ".xlsx")
                //{
                //    reader = ExcelReaderFactory.CreateOpenXmlReader(streamFile);
                //}
                //else if (extension == ".csv")
                //{
                //    reader = ExcelReaderFactory.CreateCsvReader(streamFile);
                //}

                //if (reader == null)
                //    return null;

                //using (reader)
                //{
                //    ds = reader.AsDataSet(new ExcelDataSetConfiguration()
                //    {
                //        UseColumnDataType = false,
                //        ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                //        {
                //            UseHeaderRow = true
                //        }
                //    });
                //}
                return ds;
            }
        }
    }
}
